@extends('layouts.console_header')

@section('title', '商家续签管理列表')

@section('resources')
    @parent
    <style>
        .xuyue ul{height: 168px;}
        .xuyue ul li{margin: 10px 0;}
        .xuyue ul li span{width: 75px;height: 30px;line-height: 30px;}
        .xuyue ul li input{width: 200px;height: 30px;border: 1px solid #000;}
        .xuyue ul li select{width: 200px;height: 30px;border: 1px solid #000;}
    </style>
@endsection

@section('content')
    <section class="user_list">
        <div style="border-bottom: 2px solid #9955c6;overflow: hidden;">
            <p class="fz_14 account_title fl" style="border: none;">
                <span style="color: #7f7f7f;"><a href="/console/index">首页</a>>商家版运营></span>
                <span class="co_violet">商家续签管理列表</span>
            </p>
            <a style="cursor:pointer;" onclick="export_data()"><p class="fr tc back_btn">导出</p></a>
        </div>
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
            <tr>
                <th>序号</th>
                <th>商家代码</th>
                <th>商家名称</th>
                <th>商务姓名</th>
                <th>商务账号</th>
                <th>商家状态</th>
                <th>合同到期日</th>
                <th>操作</th>
            </tr>
            </thead>
            <tfoot>
            <tr>
                <th>序号</th>
                <th>商家代码</th>
                <th>商家名称</th>
                <th>商务姓名</th>
                <th>商务账号</th>
                <th>商家状态</th>
                <th>合同到期日</th>
                <th>操作</th>
            </tr>
            </tfoot>
            <tbody>
            @if(!empty($lists))
                @foreach($lists as $list)
                    <tr>
                        @if(!isset($i))
                            <td>{{$i=1}}</td>
                        @else
                            <td>{{++$i}}</td>
                        @endif
                        <td>{{$list->shopcode}}</td>
                        <td>{{$list->shopname}}</td>
                        <td>{{$list->realname}}</td>
                        <td>{{$list->mobile}}</td>
                        @if($list->shopstatus == '1')
                            <td>上线</td>
                        @else
                            <td>下架</td>
                        @endif
                        <td>{{date('Y-m-d', strtotime($list->lasttime))}}</td>
                        <td>
                            <a href="#" style="margin: 0 10px;" onclick="renew({{$list->shopid}})">续签</a>
                            <a style="color: #9955c6;" href="/console/shop/get?shopid={{$list->shopid}}&cityid={{$cityid}}">详情</a>
                        </td>
                    </tr>
                @endforeach
            @else
                <tr><td>暂无数据</td></tr>
            @endif
            </tbody>
        </table>
        <div class="hot_search_mark xuyue" style="height: 300px;">
            <p class="oh">
                <span class="fl content">商家续约</span>
                <img class="fr close" src="{{env('IMAGE_DOMAIN')}}/images/close_p.png" alt="">
            </p>
            <ul style="padding: 0 10px;height: 188px;">
                <li style="text-align: center;">续约后，商家维护人员信息同步更新！</li>
                <li class="oh">
                    <span class="fl">续约人</span>
                    <select name="contractor" id="contractinfo">
                        {{--<option value="">张三</option>--}}
                        {{--<option value="">李四</option>--}}
                    </select>
                </li>
                <li class="oh"><span class="fl">开始日期</span><input class="fl" type="date" name="contractstarttime" value="{{date('Y-m-d', strtotime('+1day'))}}" min="{{date('Y-m-d', strtotime('+1day'))}}"></li>
                <li class="oh"><span class="fl">终止日期</span><input class="fl" type="date" name="contractendtime" value=""></li>
                <input type="hidden" name="selectshopid" value="">
                <li class="oh"><span class="contractmessage"></span></li>
            </ul>
            <p>
                <button style="background-color: #9955c6;color: #fff;line-height: 30px;" class="sure fl" id="submit_renew">确定</button>
                <button class="cancel fr" style="line-height: 30px;">取消</button>
            </p>
        </div>
    </section>

@endsection

@section('scriptResources')
    @parent
    <script>
        var cityid='{{$cityid}}';
        function renew(shopid) {
            $('.xuyue').show();
            $.ajax({
                url : '/console/admin/account/getbyrole',
                type : 'get',
                data: {
                    'cityid':cityid
                },
                dataType:'json',
                success:function(data){
                    if(data.status == 'success') {
                        var data = data.data;
                        var shop_contract = "";
                        for(var i=0; i<data.length; i++) {
                            var list = data[i];
                            shop_contract += '<option value="'+list.id+'">' + list.realname + '</option>';
                        }
                        $("#contractinfo").html(shop_contract);
                        $("input[name='selectshopid']").val(shopid);
                    } else {
                        $(".contractmessage").text(data.message);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                }
            });
            $('.cancel').click(function(){
                $('.xuyue').hide();
            })
        }
        //提交续约
        $("#submit_renew").click(function() {
            var selectshopid=$("input[name='selectshopid']").val();
            var contractstarttime=$("input[name='contractstarttime']").val();
            var contractendtime=$("input[name='contractendtime']").val();
            var contractorid = $("select[name='contractor']").find("option:selected").val();
            if(!contractendtime || !contractstarttime || !contractorid) {
                $(".contractmessage").text("填写完整数据");return;
            }
            console.log(contractstarttime)
            console.log(contractendtime)
            if (contractendtime < contractstarttime) {
                $(".contractmessage").text("终止日期必须要大于开始日期");return;
            }
            $.ajax({
                url : '/console/shop/renew',
                type : 'post',
                data: {
                    'shopid':selectshopid,
                    'cityid':cityid,
                    'contractorid': contractorid,
                    'starttime':contractstarttime,
                    'endtime': contractendtime
                },
                dataType:'json',
                success:function(data){
                    if(data.status == 'success') {
                        $('.xuyue').hide();
                        location.reload()
                    } else {
                        $(".contractmessage").text(data.message);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                }
            });
        });
        $(document).ready(function() {
            $(".close").click(function(){
                $(this).parent().parent().hide();
            })
        });

        function export_data() {
            var startTime = $("input[name='starttime']").val();
            var lastTime = $("input[name='lasttime']").val();
            var search = $("input[name='search']").val();
            var start_date = new Date(startTime);
            var last_date = new Date(lastTime);
            var differ = last_date.getTime()-start_date.getTime();
            var cityid = "{{$cityid}}";
            differ = parseInt(differ/(1000*60*60*24));
            window.open("/console/shop/lastevent/export?cityid="+cityid+"&search="+search+"&starttime="+startTime+"&lasttime="+lastTime, '_blank');
        }
    </script>
@endsection